package com.mxin.ui.init

import android.content.Context
import com.max.baselib.widget.toolbar.ToolbarConfig
import com.max.baselib.widget.toolbar.ToolbarHelper
import com.mxin.common.log.LogA
import com.mxin.ui.BuildConfig
import com.mxin.ui.R
import com.mxin.ui.loading.Gloading
import com.mxin.ui.loading.IndicatorAdapter
import com.mxin.util.AppUtil
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout

class UIManager{

    private lateinit var context: Context

    companion object{

        private val manager by lazy { UIManager() }

        fun getInstance(context:Context):UIManager{
            manager.context = context
            manager.init()
            return manager
        }

    }

    private fun init(){
        LogA.d("启动初始化 UIManager.init()")
        initGlobalTitleView()
        initGlobalLoadingView()
        initGlobalRefreshLayoutStyle()
    }


    /**
     * 初始化全局的刷新控件样式
     */
    private fun initGlobalRefreshLayoutStyle(){
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
            ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f);
        }
    }

    private fun initGlobalLoadingView(){
        Gloading.debug(BuildConfig.DEBUG)
        Gloading.initDefault(IndicatorAdapter())
    }

    private fun initGlobalTitleView(){
        ToolbarHelper.setConfig(
                ToolbarConfig.createCenterTitleConfig(
                        bgColor = AppUtil.getApp().resources.getColor(R.color.colorPrimary),
                        titleStyle = R.style.toolbarTitleStyle,
                        navigationIcon = R.drawable.ic_baseline_arrow_back_ios_24
                )
        )
    }
}